local UIBase = require("view.UIBase")
local PopupShopBuy = class("PopupShopBuy", UIBase)
local TypeHelper = require("utils.TypeHelper")
local UserDataHelper = require("data.helper.UserDataHelper")

function PopupShopBuy:ctor(shopCfg)
    self._shopCfg = shopCfg
    local resource = {
        path = "Prefabs/View/Shop",
        name = "PopupShopBuy",
        canvas = 3
    }
    PopupShopBuy.super.ctor(self, resource)
end

function PopupShopBuy:start()
    G_UIUtil.addTouchEvent(self._panelTouch.transform, nil, nil, handler(self, self._onPanelTouchClick))
    self._btnOk.onClick:AddListener(handler(self, self._onBtnOkClick))
    self._btnCancel.onClick:AddListener(handler(self, self._onBtnCancelClick))
    self._icon:updateUI(self._shopCfg.item_type, self._shopCfg.item_value, self._shopCfg.item_size)
    local cost, name = self:getTipContent()
    self._textTip.text = Lang.get("lang_shop_buy_tip", {cost = cost, name = name})
end

function PopupShopBuy:getTipContent()
    local priceCfg = TypeHelper.getCfg(self._shopCfg.price_type, self._shopCfg.price_value)
    local cost = priceCfg.name .. "x" .. self._shopCfg.price_size
    local itemName = ""
    if self._shopCfg.name == "" then
        local itemParam = self._icon:getItemParam()
        itemName = itemParam.name
    else
        itemName = self._shopCfg.name
    end
    return cost, itemName
end

function PopupShopBuy:_onBtnOkClick()
    if UserDataHelper.shopBuy(self._shopCfg) then
        G_UIManager:showTip(Lang.get("lang_shop_buy_success"))
    else
        local priceCfg = TypeHelper.getCfg(self._shopCfg.price_type, self._shopCfg.price_value)
        G_UIManager:showTip(Lang.get("lang_shop_buy_fail", {name = priceCfg.name}))
    end
    self:close()
end

function PopupShopBuy:_onBtnCancelClick()
    self:close()
end

function PopupShopBuy:_onPanelTouchClick()
    self:close()
end

return PopupShopBuy
